package com.share.tool.mock.web;

import com.share.common.common.Page;
import com.share.common.common.Result;
import com.share.tool.mock.model.db.SourceData;
import com.share.tool.mock.model.vo.SourceDataAddOrUpdateReq;
import com.share.tool.mock.model.vo.SourceDataPageReq;
import com.share.tool.mock.service.ISourceDataService;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 触发原始数据表  前端控制器
 * </p>
 *
 * @author author
 * @since 2023-11-24
 */
@RestController
@RequestMapping("/sourcedata")
public class SourceDataController {
    @Autowired
    private ISourceDataService sourceDataService;

    @ApiOperation("分页查询")
    @RequestMapping(value = "/pageList")
    public Result httpPageList(SourceDataPageReq req) {
        Page page = sourceDataService.pageList(req);
        return Result.success(page);
    }

    @ApiOperation("修改")
    @RequestMapping("/update")
    public Result update(SourceDataAddOrUpdateReq req) {
        if (req.getId() == null) {
            return Result.failure("参数异常!");
        }
        SourceData record = new SourceData();
        BeanUtils.copyProperties(req, record);
        return Result.bool(sourceDataService.updateById(record));
    }

    @ApiOperation("触发请求---生产数据-并执行请求")
    @RequestMapping("/trigerByConfId")
    public Result trigerByConfId(Long id) {
        if (id == null) {
            return Result.failure("id参数异常");
        }
        Object res = sourceDataService.trigerByConfId(id);
        return Result.success(res);
    }

    @ApiOperation("触发请求---执行一个已经存在了的请求")
    @RequestMapping("/trigerByDataId")
    public Result trigerByDataId(String id) {
        if (StringUtils.isBlank(id)) {
            return Result.failure("id参数异常");
        }
        Object res = sourceDataService.trigerByDataId(id);
        return Result.success(res);
    }
}
